//
//  HomeViewController.m
//  tianyue
//
//  Created by ZhengYing on 2017/7/5.
//  Copyright © 2017年 zy. All rights reserved.
//

#import "HomeViewController.h"
#import "PPMovieViewUI.h"
#import <AVFoundation/AVFoundation.h>
@class PPMovieViewUI;
@class MPMoviePlayerController;
@interface HomeViewController (){
    int imagecount;
    AVAudioPlayer *avPlayer;
    
    
    UIImageView *m_imageview;
    
    MPMoviePlayerController *m_player;
    PPMovieViewUI *m_ui;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollview.pagingEnabled = YES;
    self.scrollview.contentSize = CGSizeMake(1024*3, 0);
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieStatusChange:)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:nil];

    // Do any additional setup after loading the view.
}
- (IBAction)playMovie:(id)sender {
    [self gotoVideo:sender];

}
-(NSString *)getResourcePath:(NSString *)filePath
{
    NSString *path = [[NSBundle mainBundle] bundlePath];
    
    if ([filePath hasPrefix:@"/"])
    {
        path = [path stringByAppendingString:filePath];
    }
    else {
        path = [path stringByAppendingFormat:@"/%@", filePath];
    }
    return path;
}


- (void) gotoVideo: (id) sender {
    
    NSString *filePath = [self getResourcePath:@"skin/ty.mp4"];
    
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        if (!m_player)
        {
            m_player = [[MPMoviePlayerController alloc] init];
            m_player.scalingMode = MPMovieScalingModeAspectFill;
            m_player.controlStyle = MPMovieControlStyleNone;
            
            m_ui = [[PPMovieViewUI alloc] initWithFrame:CGRectMake(0, 0, 1024, 44)];
            [m_player.view addSubview:m_ui];
            m_ui.curMovieCtrl = m_player;
            
            m_player.view.frame = CGRectMake(0, 0, 1024, 768);
            m_player.view.backgroundColor = [UIColor blackColor];
            [self.view addSubview:m_player.view];
        }
        
        [m_player setContentURL:[NSURL fileURLWithPath:filePath]];
        [m_player play];
    }
}



- (void)movieStatusChange:(NSNotification *)notify
{
    MPMoviePlayerController *player = (MPMoviePlayerController *)[notify object];
    
    if (m_player == player)
    {
        if (player.playbackState == MPMoviePlaybackStateStopped)
        {
            m_ui.curMovieCtrl = nil;
            [m_ui removeFromSuperview];
            m_ui = nil;
            [m_player.view removeFromSuperview];
            m_player = nil;
            
        }
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
